package com.nsl.my_youlai.bbu_return.convert.customized;

import com.nsl.my_youlai.bbu2.enums.*;
import com.nsl.my_youlai.bbu_return.enums.*;
import com.nsl.my_youlai.bbu_return.enums.check.CheckSchemeExcelRecordErrorType;
import com.nsl.my_youlai.bbu_return.enums.check.VendorBbuModelEnum;
import com.nsl.my_youlai.bbu_return.exception.EnumTransformException;
import com.nsl.my_youlai.bbu_return.model.bo.check.CheckSchemeExcelRecord;
import com.nsl.my_youlai.bbu_return.model.bo.SchemeExcelRecordBo;
import com.nsl.my_youlai.bbu_return.model.vo.SchemeExcelRecordString;
import com.nsl.my_youlai.common.base.IBaseEnum;
import com.nsl.my_youlai.mystar.enums.NslEnumBaseI;
import com.nsl.my_youlai.mystar.enums.NslEnumTransformException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class SchemeExcelRecordCustomizedConverter {

   public static void toBoList(List<SchemeExcelRecordString> voStringList,List<SchemeExcelRecordBo> boList,List<CheckSchemeExcelRecord> checkSchemeExcelRecordList){
       for (int i = 0; i < voStringList.size(); i++) {
           SchemeExcelRecordString voString=voStringList.get(i);
           boList.set(i,toBo(voString,boList.get(i),checkSchemeExcelRecordList));
       }
   }

   public static SchemeExcelRecordBo toBo(SchemeExcelRecordString voString,SchemeExcelRecordBo bo, List<CheckSchemeExcelRecord> checkSchemeExcelRecordList){
        Integer rowIndex= voString.getRowIndex();

       String nodeId=voString.getNodeBId();

       List<String> requiredColumnsCheckResultList=requiredColumnsNull2(voString);


       //方案为空，跳过检查
//       if(!NslUtilString.notNullAndTrimBlank(voString.getAction())){
//           CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
//           checkSchemeExcelRecord.setRowIndex(rowIndex);
//           checkSchemeExcelRecord.setSkip(true);
//           bo.setSkip(true);
//       }

       if(requiredColumnsCheckResultList.size()>0){
           CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
           checkSchemeExcelRecord.setRowIndex(rowIndex);
           checkSchemeExcelRecord.setType("错误");
           checkSchemeExcelRecord.setTitle("必填项缺失:"+requiredColumnsCheckResultList);
           checkSchemeExcelRecord.setExcelInputString(nodeId);
           checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.REQUIRED_COLUMNS_NULL_ERROR);
           checkSchemeExcelRecordList.add(checkSchemeExcelRecord);

       }

//       if(requiredColumnsNull(voString)){
//           CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
//           checkSchemeExcelRecord.setRowIndex(rowIndex);
//           checkSchemeExcelRecord.setType("错误");
//           checkSchemeExcelRecord.setTitle("必填项缺失");
//           checkSchemeExcelRecord.setExcelInputString(nodeId);
//           checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.REQUIRED_COLUMNS_NULL_ERROR);
//           checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
//       }


       try{
           Integer nodeIdInteger=Integer.parseInt(nodeId);
           bo.setNodeId(nodeIdInteger);
       }catch (NumberFormatException numberFormatException){
           CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
           checkSchemeExcelRecord.setRowIndex(rowIndex);
           checkSchemeExcelRecord.setType("错误");
           if(voString.getNodeBId()!=null && voString.getNodeBId().trim().equals("不确定")){
               checkSchemeExcelRecord.setTitle("nodeB id 不确定_s");
               checkSchemeExcelRecord.setExcelInputString("nodeB id 不确定_s");
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.BBU_ID_UNKNOWN);
           }else{
               checkSchemeExcelRecord.setTitle("nodeBId 错误");
               checkSchemeExcelRecord.setExcelInputString(nodeId);
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.NODE_iD_ERROR);
           }
           checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
       }

       //tac
       try{
           if(voString.getSourceTac()!=null && !voString.getSourceTac().trim().equals("")){
               Integer tacInteger=Integer.parseInt(voString.getSourceTac());
               bo.setSourceTac(tacInteger);
           }
       }catch (NumberFormatException numberFormatException){
           CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
           checkSchemeExcelRecord.setRowIndex(rowIndex);
           checkSchemeExcelRecord.setType("错误");
           if(voString.getSourceTac()!=null && voString.getSourceTac().trim().equals("不确定")){
               checkSchemeExcelRecord.setTitle("Tac: 不确定_a");
               checkSchemeExcelRecord.setExcelInputString("Tac: 不确定_a");
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.TAC_UNKNOWN);
           }else{
               checkSchemeExcelRecord.setTitle("Tac:"+voString.getSourceTac() +" 非数字");
               checkSchemeExcelRecord.setExcelInputString(nodeId);
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.TAC_NOT_NUMBER_ERROR);
           }
           checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
       }


       //不准
       String generationTypeString=voString.getGenerationType();
       bo.setGenerationType(string2Enum(rowIndex,generationTypeString, GenerationTypeEnum.class,checkSchemeExcelRecordList, CheckSchemeExcelRecordErrorType.GENERATION_TYPE_ERROR));

       String action=voString.getAction();
       bo.setAction(string2Enum(rowIndex,action, ActionEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.ACTION_ERROR));
//
       String boardType=voString.getBoardType();
       bo.setBoardType(string2Enum(rowIndex,boardType, BoardTypeEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.BOARD_TYPE_ERROR));

       String bbuModel=voString.getBbuModel();
       bo.setBbuModel(string2Enum(rowIndex,bbuModel, BbuModelEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.BBU_MODEL_ERROR));

        String vendor=voString.getVendor();
        VendorEnum vendorEnum=string2Enum(rowIndex,vendor, VendorEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.VENDOR_ERROR);
        bo.setVendor(vendorEnum);

        //检查地市字段
       String city=voString.getCity();
       CityEnum cityEnum=string2Enum(rowIndex,city, CityEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.CITY_ERROR);

       //检查机房归属
       String ownership=voString.getOwnership();
       ApparatusRoomOwnershipEnum apparatusRoomOwnershipEnum =string2Enum(rowIndex,ownership, ApparatusRoomOwnershipEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.MACHINE_ROOM_OWNERSHIP_ERROR);

       //检查 本站机房归类
       String machineRoomCategory=voString.getRoomCategory();
       ApparatusRoomCategoryEnum apparatusRoomCategoryEnum =string2Enum(rowIndex,machineRoomCategory, ApparatusRoomCategoryEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.MACHINE_ROOM_CATEGORY_ERROR);

       //检查 室分or宏站
       String macroOrMicro=voString.getMacroOrMicro();
       IndoorOutdoorEnum indoorOutdoorEnum=string2Enum(rowIndex,macroOrMicro, IndoorOutdoorEnum.class,checkSchemeExcelRecordList,CheckSchemeExcelRecordErrorType.INDOOR_OUTDOOR_ERROR);


       //槽道 -1 表示是机框和风扇,特殊处理
       if(bo.getBoardType()!=null && bo.getBoardType().equals(BoardTypeEnum.FUN)){
           bo.setSlotIndex(-1);
       }

        //槽道，如果 boardType 不为 风扇+机框，必须有槽道，并且为数值
       if(bo.getBoardType()!=null && !bo.getBoardType().equals(BoardTypeEnum.FUN)){
           String slotIndex= voString.getSlotIndex();
           try {
               bo.setSlotIndex(Integer.parseInt(slotIndex));
           }catch (NumberFormatException numberFormatException){
               CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
               checkSchemeExcelRecord.setRowIndex(rowIndex);
               checkSchemeExcelRecord.setType("错误");
               checkSchemeExcelRecord.setTitle("槽道错误");
               checkSchemeExcelRecord.setExcelInputString(slotIndex);
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.REQUIRED_SLOT_INDEX_ERROR);
               checkSchemeExcelRecordList.add(checkSchemeExcelRecord);

           }
       }

       //如果action为DISMANTLE_SETUP 拆除-回局安装 或者 DISMANTLE_SETUP_OF_ANCHOR 拆除-锚点内安装 ，则新bbu ID 和 新槽道为 空，新tac有的话，按新，无，按旧
       if(bo.getAction()!=null && (bo.getAction().equals(ActionEnum.DISMANTLE_SETUP) || bo.getAction().equals(ActionEnum.DISMANTLE_SETUP_OF_ANCHOR))){
            if(voString.getDestinationNodeBId()!=null || voString.getDestinationSlotIndex()!=null || (voString.getDestinationSlotIndex()!=null && voString.getDestinationSlotIndex().trim()!="") || (voString.getDestinationTac()!=null && voString.getDestinationNodeBId().trim()!="")){
                CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
                checkSchemeExcelRecord.setRowIndex(rowIndex);
                checkSchemeExcelRecord.setType("错误");
                checkSchemeExcelRecord.setTitle("AF-AH一般应为空_a");
                checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.DISMANTLE_SETUP_ACTION_NEW_BBU_ID_AND_SLOT_BLANK_ERROR);
                checkSchemeExcelRecord.setExcelInputString("AF-AH一般应为空_a");
                checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
            }
           bo.setDestinationNodeBId(bo.getNodeId());
           bo.setDestinationSlotIndex(bo.getSlotIndex());
           if(voString.getDestinationTac()==null || voString.getDestinationTac().trim().equals("")){
               bo.setDestinationTac(voString.getSourceTac());
           }else{
               bo.setDestinationTac(voString.getDestinationTac());
           }

       }


       //如果action为 拆除-锚点内整合 或 拆除-回局整合，必须有目标node id 和 slot index
       if(bo.getAction()!=null && (bo.getAction().equals(ActionEnum.DISMANTLE_INTEGRATION)||bo.getAction().equals(ActionEnum.DISMANTLE_INTEGRATION_OF_ANCHOR))){
           int destinationNodeId=-1;
           int destinationSlotIndex=-1;
           try{
               destinationNodeId=Integer.parseInt(voString.getDestinationNodeBId());
               if(bo.getBoardType()!=null && !bo.getBoardType().equals(BoardTypeEnum.FUN)){
                   destinationSlotIndex=Integer.parseInt(voString.getDestinationSlotIndex());
               }
               //log.info("voString.getDestinationSlotIndex() = {}",destinationSlotIndex);
           }catch (NumberFormatException numberFormatException){
               CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
               checkSchemeExcelRecord.setRowIndex(rowIndex);
               checkSchemeExcelRecord.setType("错误");
               checkSchemeExcelRecord.setTitle("整合至新BBU ID 或 整合至新BBU槽道号 错误");
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.REQUIRED_DESTINATION_ID_AND_SLOT_INDEX_ERROR);
               checkSchemeExcelRecord.setExcelInputString(destinationNodeId+" : "+destinationSlotIndex);
               checkSchemeExcelRecordList.add(checkSchemeExcelRecord);


           }
           bo.setDestinationNodeBId(destinationNodeId);
           bo.setDestinationSlotIndex(destinationSlotIndex);

       }

        //检查 厂家和bbu型号 的匹配
       if(bo.getBbuModel()!=null && bo.getVendor()!=null ){
           String vendorBbuModel=bo.getVendor().getLabel()+"_"+bo.getBbuModel().getLabel();
           //log.info("vendorBbuModel = {}",vendorBbuModel);
           try {
               VendorBbuModelEnum vendorBbuModelEnum=IBaseEnum.getEnumByLabel(vendorBbuModel,VendorBbuModelEnum.class);
               bo.setVendorBbuModelEnum(vendorBbuModelEnum);
           } catch (EnumTransformException e) {
               CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
               checkSchemeExcelRecord.setRowIndex(rowIndex);
               checkSchemeExcelRecord.setType("错误");
               checkSchemeExcelRecord.setTitle("厂家和BBU型号不匹配");
               checkSchemeExcelRecord.setExcelInputString(bo.getVendor().getLabel()+" , "+bo.getBbuModel().getLabel());
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.BBU_VERDOR_MATCH);
               checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
           }
       }

       if(bo.getBoardType()!=null && bo.getVendor()!=null){
           BoardTypeEnum boardTypeEnum=bo.getBoardType();
           VendorEnum vendorEnumA=bo.getVendor();
           if(boardTypeEnum==BoardTypeEnum.SWITCH_BOARD && vendorEnumA!=VendorEnum.ZX){
               CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
               checkSchemeExcelRecord.setRowIndex(rowIndex);
               checkSchemeExcelRecord.setType("错误");
               checkSchemeExcelRecord.setTitle("仅中兴可以匹配交换板");
               checkSchemeExcelRecord.setExcelInputString(bo.getVendor().getLabel()+" , "+bo.getBoardType().getLabel());
               checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(CheckSchemeExcelRecordErrorType.VENDOR_SWITCH_BOARD_ERROR);
               checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
           }
       }

        return bo;


    }

    static <E extends Enum<E> & NslEnumBaseI> E  string2Enum(Integer rowIndex, String string, Class<E> tClass, List<CheckSchemeExcelRecord> checkSchemeExcelRecordList, CheckSchemeExcelRecordErrorType checkSchemeExcelRecordErrorType){
        E e= null;
        if(string==null || string.trim().equals("")){

            return null;
        }

        try {
            e = (E) NslEnumBaseI.getEnumByValue(string,tClass);
        } catch (NslEnumTransformException ex) {
            CheckSchemeExcelRecord checkSchemeExcelRecord=new CheckSchemeExcelRecord();
            checkSchemeExcelRecord.setRowIndex(rowIndex);
            checkSchemeExcelRecord.setType("错误");
            checkSchemeExcelRecord.setTitle(ex.getTClass().getName());
            checkSchemeExcelRecord.setExcelInputString(string);
            checkSchemeExcelRecord.setCheckSchemeExcelRecordErrorType(checkSchemeExcelRecordErrorType);
            checkSchemeExcelRecordList.add(checkSchemeExcelRecord);
            //throw new RuntimeException(ex);
        }
        return e;
    }

    private static boolean requiredColumnsNull(SchemeExcelRecordString voString){
//       if(voStringList.getGenerationType()==null || voStringList.getGenerationType().trim().equals("")){
//           return true;
//       }

        if(voString.getBoardSn()==null || voString.getBoardSn().trim().equals("")){
            String boardType=voString.getBoardType();
            if(!boardType.equalsIgnoreCase(BoardTypeEnum.FUN.getLabel())){
                return true;
            }

        }

        if(voString.getAction()==null || voString.getAction().trim().equals("")){
            return true;
        }
        if(voString.getBbuModel()==null || voString.getBbuModel().trim().equals("")){
            return true;
        }
        if(voString.getBoardType()==null || voString.getBoardType().trim().equals("")){
            return true;
        }
        if(voString.getNodeBId()==null || voString.getNodeBId().trim().equals("")){
            return true;
        }

        return false;
    }

    private static List<String> requiredColumnsNull2(SchemeExcelRecordString voString){
//       if(voStringList.getGenerationType()==null || voStringList.getGenerationType().trim().equals("")){
//           return true;
//       }

        List<String> requiredColumnsList=new ArrayList<>();


        if(voString.getBoardSn()==null || voString.getBoardSn().trim().equals("")){
            String boardType=voString.getBoardType();
            //log.info("voString= {}",voString);
            if(!boardType.equalsIgnoreCase(BoardTypeEnum.FUN.getLabel())){
                requiredColumnsList.add("S/N码");
            }

        }

        if(voString.getVendor()==null || voString.getVendor().trim().equals("")){
            requiredColumnsList.add(VendorEnum.ZX.getCnName());
        }

        if(voString.getAction()==null || voString.getAction().trim().equals("")){
            requiredColumnsList.add(ActionEnum.UNCHANGED.getCnName());
        }
        if(voString.getBbuModel()==null || voString.getBbuModel().trim().equals("")){
            requiredColumnsList.add(BbuModelEnum.BBU_3900.getCnName());
        }
        if(voString.getBoardType()==null || voString.getBoardType().trim().equals("")){
            requiredColumnsList.add(BoardTypeEnum.FUN.getCnName());
        }

        if(voString.getCity()==null || voString.getCity().trim().equals("")){
            requiredColumnsList.add(CityEnum.AN_YANG.getCnName());
        }

        if(voString.getOwnership()==null || voString.getOwnership().trim().equals("")){
            requiredColumnsList.add(ApparatusRoomOwnershipEnum.TOWER.getCnName());
        }

        if(voString.getRoomCategory()==null || voString.getRoomCategory().trim().equals("")){
            requiredColumnsList.add(ApparatusRoomCategoryEnum.AGGREGATION_MACHINE_ROOM.getCnName());
        }

        if(voString.getMacroOrMicro()==null || voString.getMacroOrMicro().trim().equals("")){
            requiredColumnsList.add(IndoorOutdoorEnum.INDOOR.getCnName());
        }

        if(voString.getOwnership()==null || voString.getOwnership().trim().equals("")){
            requiredColumnsList.add(ApparatusRoomOwnershipEnum.TOWER.getCnName());
        }


        if(voString.getNodeBId()==null || voString.getNodeBId().trim().equals("")){
            requiredColumnsList.add("NodeB_Id");
        }

        if(voString.getSourceTac()==null || voString.getSourceTac().trim().equals("")){
            requiredColumnsList.add("原BBU归属TAC");
        }

        return requiredColumnsList;
    }
}
